
/**
 * @author LKQ
 * @date 2021/12/3 17:56
 * @description
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.addBinary("1", "111"));
    }

    public String addBinary(String a, String b) {
        int longest = Math.max(a.length(), b.length());
        int shortest = Math.min(a.length(), b.length());
        // 进位标志
        int add = 0;
        String[] arr = new String[longest];
        for (int i = longest-1; i >= 0; i--) {
            int number = 0;
            if (longest == a.length()) {
                // a字符串长
                int numberA = Integer.parseInt(String.valueOf(a.charAt(i)));
                if (shortest <= 0) {
                    number = numberA;
                }else {
                    number = numberA + Integer.parseInt(String.valueOf(b.charAt(shortest-1)));
                }
            }else {
                // b 字符串长
                int numberB = Integer.parseInt(String.valueOf(b.charAt(i)));
                if (shortest <= 0) {
                    number = numberB;
                }else {
                    number = numberB + Integer.parseInt(String.valueOf(a.charAt(shortest-1)));
                }
            }
            int temp = number + add;
            if (temp >= 2) {
                arr[i] = String.valueOf(temp-2);
                add = 1;
            }else {
                arr[i] = String.valueOf(temp);
                add = 0;
            }
            shortest--;
        }
        String x = "";
        if (add == 1) {
            x = "1";
        }
        for (String temp:arr) {
            x += temp;
        }
        return x;
    }
}
